
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content>
  

  
    <meta name="description" content="Vue的简单入门">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>Vue的简单入门 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        Vue的简单入门
      </h1>
      <span>
        
        <time class="time" datetime="2019-01-08T16:00:00.000Z">
        2019-01-09
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h3 id="基本介绍"><a href="#基本介绍" class="headerlink" title="基本介绍"></a>基本介绍</h3><p> 1、Vue.js目前最火的的一个前端框架，三大主流前端框架之一。<br> 2、Vue.js是一套构建用户界面的框架（一套完整的解决方案，对项目侵入性大，中途需要跟换框架则需要重构整个项目），只关注视图层，易上手，有配套的第三方类库。<br> 3、提高开发效率，帮助减少不必要的dom操作；双向数据绑定，通过框架提供的指令，前端只需要关注业务逻辑，不再关心dom如何渲染。</p>
<h4 id="作者：尤雨熙"><a href="#作者：尤雨熙" class="headerlink" title="作者：尤雨熙"></a>作者：尤雨熙</h4><h4 id="官方文档"><a href="#官方文档" class="headerlink" title="官方文档"></a>官方文档</h4><blockquote>
<p><a href="https://cn.vuejs.org/" target="_blank" rel="noopener">https://cn.vuejs.org/</a></p>
</blockquote>
<h3 id="快速入门"><a href="#快速入门" class="headerlink" title="快速入门"></a>快速入门</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 导入vue的包 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.jsdelivr.net/npm/vue/dist/vue.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 将来的Vue实例会控制这个元素中所有内容 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- V（View），Vue实例所控制的这个元素区域 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span>   </span><br><span class="line">            <span class="comment">&lt;!-- 通过Vue提供的指令，很方便就能把data里的数据渲染到页面，不用手动操作dom了</span></span><br><span class="line"><span class="comment">           （前端Vue之类的框架，不提倡去手动操作dom） --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; msg &#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line">            //导入包,就有Vue的构造函数</span><br><span class="line">            //VM,这个new出来的vm对象,就是调度者</span><br><span class="line">            var vm = new Vue(&#123;   //&#123;&#125;配置对象</span><br><span class="line">                el:'#app',   //element,这个Vue实例要控制页面上的哪个区域</span><br><span class="line">                data:&#123;   //M,data就是用来保存当前页面数据的</span><br><span class="line">                    msg:'欢迎学习Vue'</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="el的作用范围"><a href="#el的作用范围" class="headerlink" title="el的作用范围"></a>el的作用范围</h3><h4 id="选择器一定是id选择器吗？"><a href="#选择器一定是id选择器吗？" class="headerlink" title="选择器一定是id选择器吗？"></a>选择器一定是id选择器吗？</h4><blockquote>
<p>不一定是，可以是id选择器或者类选择器以及标签选择器</p>
</blockquote>
<h4 id="可以使用其他标签吗？"><a href="#可以使用其他标签吗？" class="headerlink" title="可以使用其他标签吗？"></a>可以使用其他标签吗？</h4><blockquote>
<p>只能是双标签中的内容，除了body和html</p>
</blockquote>
<h4 id="作用的范围是哪里？"><a href="#作用的范围是哪里？" class="headerlink" title="作用的范围是哪里？"></a>作用的范围是哪里？</h4><blockquote>
<p>命中的元素以及后代子孙元素</p>
</blockquote>
<h3 id="小总结"><a href="#小总结" class="headerlink" title="小总结"></a>小总结</h3><blockquote>
<p>创建Vue实例的时候，el(挂载点)，data(数据)，methods(设置方法)</p>
</blockquote>
<h2 id="vue中常见的指令"><a href="#vue中常见的指令" class="headerlink" title="vue中常见的指令"></a>vue中常见的指令</h2><h3 id="v-text的用法："><a href="#v-text的用法：" class="headerlink" title="v-text的用法："></a>v-text的用法：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=&quot;en&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue/dist/vue.js&quot;&gt;&lt;/script&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div id=&quot;test&quot;&gt;</span><br><span class="line">    &lt;h2 v-text=&quot;message&quot;&gt;&lt;/h2&gt;</span><br><span class="line">    &lt;h3 v-text=&quot;info&quot;&gt;&lt;/h3&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">  &lt;script&gt;</span><br><span class="line">    var app2 = new Vue(&#123;</span><br><span class="line">        el: &apos;#test&apos;,</span><br><span class="line">        data:&#123;</span><br><span class="line">            message: &quot;this is test&quot;,</span><br><span class="line">            info: &quot;this is info&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &lt;/script&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<h3 id="v-html的使用"><a href="#v-html的使用" class="headerlink" title="v-html的使用"></a>v-html的使用</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&quot;test&quot;&gt;</span><br><span class="line">   &lt;h4 v-html=&quot;myhtml&quot;&gt;&lt;/h4&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line">	var app2 = new Vue(&#123;</span><br><span class="line">       el: &apos;#test&apos;,</span><br><span class="line">       data:&#123;</span><br><span class="line">           myhtml: &quot;&lt;a href=&apos;http://wwww.baidu.com&apos;&gt;this is myhtml&lt;/a&gt;&quot;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;)</span><br></pre></td></tr></table></figure>

<h3 id="v-on的使用"><a href="#v-on的使用" class="headerlink" title="v-on的使用"></a>v-on的使用</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&apos;test&apos;&gt;</span><br><span class="line">  &lt;input type=&quot;button&quot; value=&quot;点击&quot; v-on:click=&quot;testclick&quot;&gt;</span><br><span class="line">  &lt;input type=&quot;button&quot; value=&quot;点击&quot; @click=&quot;testclick&quot;&gt;</span><br><span class="line">  &lt;input type=&quot;button&quot; value=&quot;点击&quot; @click=&quot;getfood&quot;&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">var app2 = new Vue(&#123;</span><br><span class="line">        el: &apos;#test&apos;,</span><br><span class="line">        data:&#123;</span><br><span class="line">            food: &quot;xxxxxxx&quot;,</span><br><span class="line">        &#125;,</span><br><span class="line">        methods :&#123;</span><br><span class="line">            testclick:function()&#123;</span><br><span class="line">              console.log(&apos;dsadas&apos;);</span><br><span class="line">            &#125;,</span><br><span class="line">            getfood:function () &#123;</span><br><span class="line">                console.log(this.food);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h3 id="v-show的使用"><a href="#v-show的使用" class="headerlink" title="v-show的使用"></a>v-show的使用</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&quot;app&quot;&gt;</span><br><span class="line">   &lt;h2 v-show=&quot;isShow&quot;&gt;hello,h2&lt;/h2&gt;</span><br><span class="line"> &lt;/div&gt;</span><br><span class="line"></span><br><span class="line"> &lt;script&gt;</span><br><span class="line">   var app = new Vue(&#123;</span><br><span class="line">     el: &quot;#app&quot;,</span><br><span class="line">     data:&#123;</span><br><span class="line">         num : 0,</span><br><span class="line">         isShow: false</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>简单的例子：</p>
<img src="/Users/shangzekai/Library/Application Support/typora-user-images/image-20201110221913803.png" alt="image-20201110221913803" style="zoom:50%;">

<h3 id="v-bind的使用"><a href="#v-bind的使用" class="headerlink" title="v-bind的使用"></a>v-bind的使用</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">  &lt;div id=&quot;app&quot;&gt;</span><br><span class="line">&lt;!--    &lt;h2 v-show=&quot;isShow&quot;&gt;hello,h2&lt;/h2&gt;--&gt;</span><br><span class="line">&lt;!--    &lt;img v-bind:src=&quot;imgSrc&quot; alt=&quot;&quot;&gt;--&gt;</span><br><span class="line">    &lt;img :src=&quot;imgSrc&quot; :alt=&quot;imgAlt&quot;&gt;</span><br><span class="line"></span><br><span class="line">  &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">  &lt;script&gt;</span><br><span class="line">    var app = new Vue(&#123;</span><br><span class="line">      el: &quot;#app&quot;,</span><br><span class="line">      data:&#123;</span><br><span class="line">          imgSrc: &quot;https://www.baidu.com/img/PC_803382a656108089122dc9a79d2729e8.png&quot;,</span><br><span class="line">          imgAlt : &quot;baidu&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    &lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h4 id="v-for的使用"><a href="#v-for的使用" class="headerlink" title="v-for的使用"></a>v-for的使用</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&quot;app&quot;&gt;</span><br><span class="line">   &lt;ul&gt;</span><br><span class="line">     &lt;li v-for=&quot;item in arr&quot;&gt;&#123;&#123;item&#125;&#125;&lt;/li&gt;</span><br><span class="line">   &lt;/ul&gt;</span><br><span class="line">   &lt;ul&gt;</span><br><span class="line">     &lt;li v-for=&quot;(item, index) in objArr&quot;&gt;</span><br><span class="line">       &#123;&#123;item.name&#125;&#125;</span><br><span class="line">     &lt;/li&gt;</span><br><span class="line">   &lt;/ul&gt;</span><br><span class="line"></span><br><span class="line"> &lt;/div&gt;</span><br><span class="line"></span><br><span class="line"> &lt;script&gt;</span><br><span class="line">   var app = new Vue(&#123;</span><br><span class="line">     el: &quot;#app&quot;,</span><br><span class="line">     data:&#123;</span><br><span class="line">         arr: [1,2,3,4],</span><br><span class="line">         objArr: [</span><br><span class="line">             &#123;&quot;name&quot;:&quot;szk&quot;&#125;,</span><br><span class="line">             &#123;&quot;name&quot;:&quot;zhangsan&quot;&#125;</span><br><span class="line">         ]</span><br><span class="line">     &#125;</span><br><span class="line">  &lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h4 id="v-model的使用"><a href="#v-model的使用" class="headerlink" title="v-model的使用"></a>v-model的使用</h4><blockquote>
<p>获取和设置表单元素的值（双向数据绑定）</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&apos;app&apos;&gt; </span><br><span class="line">	&lt;input type=&quot;text&quot; v-model=&quot;message&quot;&gt;</span><br><span class="line">  &lt;h2&gt;&#123;&#123; message &#125;&#125;&lt;/h2&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">  &lt;script&gt;</span><br><span class="line">    var app = new Vue(&#123;</span><br><span class="line">        el: &quot;#app&quot;,</span><br><span class="line">        data:&#123;</span><br><span class="line">            &quot;message&quot;: &quot;helloworld&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>总结：v-on绑定交互事件， v-for 循环数据， v-model 表单的双向绑定</p>
<h3 id="案例：简单记事本的实现"><a href="#案例：简单记事本的实现" class="headerlink" title="案例：简单记事本的实现"></a>案例：简单记事本的实现</h3><h4 id="新增"><a href="#新增" class="headerlink" title="新增"></a>新增</h4><ul>
<li>生成列表结构(v-for 数组)</li>
<li>获取用户的输入(v-model)</li>
<li>实现新增数据(v-on)</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&quot;todoapp&quot;&gt;</span><br><span class="line"></span><br><span class="line">  &lt;input type=&quot;text&quot; v-model=&quot;inputValue&quot;&gt;</span><br><span class="line">  &lt;input type=&quot;button&quot; value=&quot;添加&quot; @click=&quot;addTodo&quot;&gt;</span><br><span class="line">  &lt;ul&gt;</span><br><span class="line">    &lt;li v-for=&quot;item in todolist&quot;&gt;</span><br><span class="line">      &#123;&#123;item&#125;&#125;</span><br><span class="line">    &lt;/li&gt;</span><br><span class="line">  &lt;/ul&gt;</span><br><span class="line">  总共数据是&#123;&#123;todolist.length&#125;&#125;个</span><br><span class="line"></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">  var app = new Vue(&#123;</span><br><span class="line">      el: &quot;#todoapp&quot;,</span><br><span class="line">      data: &#123;</span><br><span class="line">          todolist: [&quot;吃饭&quot;, &quot;睡觉&quot;, &quot;打豆豆&quot;],</span><br><span class="line">          inputValue:&quot;&quot;</span><br><span class="line">      &#125;,</span><br><span class="line">      methods:&#123;</span><br><span class="line">          addTodo: function () &#123;</span><br><span class="line">              this.todolist.push(this.inputValue)</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h3 id="axios的使用"><a href="#axios的使用" class="headerlink" title="axios的使用"></a>axios的使用</h3><blockquote>
<p><a href="http://www.axios-js.com/zh-cn/docs/" target="_blank" rel="noopener">http://www.axios-js.com/zh-cn/docs/</a></p>
</blockquote>
<p>中文文档已经说的非常的详细了，大家可以参考中文文档！</p>
<p>需要掌握：</p>
<ul>
<li>发送请求的，一般项目经常使用axios调用后端的api</li>
<li>axios的get和post的使用要掌握</li>
<li>axios的拦截器要知道用法以及流程</li>
</ul>
<h3 id="跨域的问题："><a href="#跨域的问题：" class="headerlink" title="跨域的问题："></a>跨域的问题：</h3><p>![image-20201114171127658](/Users/shangzekai/Library/Application Support/typora-user-images/image-20201114171127658.png)</p>
<h4 id="一、为什么会出现跨域问题"><a href="#一、为什么会出现跨域问题" class="headerlink" title="一、为什么会出现跨域问题"></a>一、为什么会出现跨域问题</h4><p>出于浏览器的同源策略限制。同源策略（Sameoriginpolicy）是一种约定，它是浏览器最核心也最基本的安全功能，如果缺少了同源策略，则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的，浏览器只是针对同源策略的一种实现。同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。</p>
<p>所谓同源（即指在同一个域）就是两个页面具有相同的协议（protocol），主机（host）和端口号（port）</p>
<h4 id="二、什么是跨域"><a href="#二、什么是跨域" class="headerlink" title="二、什么是跨域"></a>二、什么是跨域</h4><blockquote>
<p>当一个请求url的<strong>协议、域名、端口</strong>三者之间任意一个与当前页面url不同即为跨域</p>
</blockquote>
<table>
<thead>
<tr>
<th><strong>当前页面url</strong></th>
<th><strong>被请求页面url</strong></th>
<th><strong>是否跨域</strong></th>
<th><strong>原因</strong></th>
</tr>
</thead>
<tbody><tr>
<td><a href="http://www.test.com/" target="_blank" rel="noopener">http://www.test.com/</a></td>
<td><a href="http://www.test.com/index.html" target="_blank" rel="noopener">http://www.test.com/index.html</a></td>
<td>否</td>
<td>同源（协议、域名、端口号相同）</td>
</tr>
<tr>
<td><a href="http://www.test.com/" target="_blank" rel="noopener">http://www.test.com/</a></td>
<td><a href="https://www.test.com/index.html" target="_blank" rel="noopener">https://www.test.com/index.html</a></td>
<td>跨域</td>
<td>协议不同（http/https）</td>
</tr>
<tr>
<td><a href="http://www.test.com/" target="_blank" rel="noopener">http://www.test.com/</a></td>
<td><a href="http://www.baidu.com/" target="_blank" rel="noopener">http://www.baidu.com/</a></td>
<td>跨域</td>
<td>主域名不同（test/baidu）</td>
</tr>
<tr>
<td><a href="http://www.test.com/" target="_blank" rel="noopener">http://www.test.com/</a></td>
<td><a href="http://blog.test.com/" target="_blank" rel="noopener">http://blog.test.com/</a></td>
<td>跨域</td>
<td>子域名不同（www/blog）</td>
</tr>
<tr>
<td><a href="http://www.test.com:8080/" target="_blank" rel="noopener">http://www.test.com:8080/</a></td>
<td><a href="http://www.test.com:7001/" target="_blank" rel="noopener">http://www.test.com:7001/</a></td>
<td>跨域</td>
<td>端口号不同（8080/7001）</td>
</tr>
</tbody></table>
<p>参考：<a href="http://www.ruanyifeng.com/blog/2016/04/cors.html" target="_blank" rel="noopener">http://www.ruanyifeng.com/blog/2016/04/cors.html</a></p>
<p>面试题会问，简单的介绍一下跨域是啥？怎么解决的？</p>
<ul>
<li>jsonp 解决</li>
<li>django中使用开源组件 django-cors-headers</li>
</ul>
<h4 id="三、Django解决跨域问题"><a href="#三、Django解决跨域问题" class="headerlink" title="三、Django解决跨域问题"></a>三、Django解决跨域问题</h4><blockquote>
<p><a href="https://github.com/adamchainz/django-cors-headers" target="_blank" rel="noopener">https://github.com/adamchainz/django-cors-headers</a></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 跨域设置</span></span><br><span class="line"><span class="keyword">from</span> corsheaders.defaults <span class="keyword">import</span> default_headers</span><br><span class="line"></span><br><span class="line">INSTALLED_APPS = [</span><br><span class="line">    ...</span><br><span class="line">    <span class="string">'corsheaders'</span>,</span><br><span class="line">    ...</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">MIDDLEWARE = [</span><br><span class="line">    ...</span><br><span class="line">    <span class="string">'corsheaders.middleware.CorsMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.middleware.common.CommonMiddleware'</span>,</span><br><span class="line">    ...</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">CORS_ALLOW_HEADERS = list(default_headers) + [</span><br><span class="line">    <span class="string">'XMLHttpRequest'</span>,</span><br><span class="line">    <span class="string">'X_FILENAME'</span>,</span><br><span class="line">    <span class="string">'accept-encoding'</span>,</span><br><span class="line">    <span class="string">'authorization'</span>,</span><br><span class="line">    <span class="string">'content-type'</span>,</span><br><span class="line">    <span class="string">'dnt'</span>,</span><br><span class="line">    <span class="string">'origin'</span>,</span><br><span class="line">    <span class="string">'user-agent'</span>,</span><br><span class="line">    <span class="string">'x-csrftoken'</span>,</span><br><span class="line">    <span class="string">'x-requested-with'</span>,</span><br><span class="line">    <span class="string">'Pragma'</span>,</span><br><span class="line">    <span class="string">'TOKEN'</span>,</span><br><span class="line">]</span><br><span class="line">CORS_ORIGIN_ALLOW_ALL = <span class="literal">True</span></span><br><span class="line">CORS_ALLOW_CREDENTIALS = <span class="literal">True</span></span><br></pre></td></tr></table></figure>

<p>传统的风格：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">path(<span class="string">'v1/api/addbook/'</span>, views.addbook),</span><br><span class="line">path(<span class="string">'v1/api/booklist/'</span>, views.booklist),</span><br></pre></td></tr></table></figure>

<p>Restful 设计风格：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">path(<span class="string">'v1/api/Book/'</span>, views.Book.as_view)</span><br></pre></td></tr></table></figure>

<p>前端发请求方式：</p>
<p>Method: Get  v1/api/Book/   代表  获取所有的书籍</p>
<p>Method: post  v1/api/Book/   代表增加数据</p>
<p>method：delete v1/api/Book/</p>

    </div>

  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#基本介绍"><span class="toc-text">基本介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#作者：尤雨熙"><span class="toc-text">作者：尤雨熙</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#官方文档"><span class="toc-text">官方文档</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#快速入门"><span class="toc-text">快速入门</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#el的作用范围"><span class="toc-text">el的作用范围</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#选择器一定是id选择器吗？"><span class="toc-text">选择器一定是id选择器吗？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#可以使用其他标签吗？"><span class="toc-text">可以使用其他标签吗？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#作用的范围是哪里？"><span class="toc-text">作用的范围是哪里？</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#小总结"><span class="toc-text">小总结</span></a></li></ol><li class="toc-item toc-level-2"><a class="toc-link" href="#vue中常见的指令"><span class="toc-text">vue中常见的指令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#v-text的用法："><span class="toc-text">v-text的用法：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-html的使用"><span class="toc-text">v-html的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-on的使用"><span class="toc-text">v-on的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-show的使用"><span class="toc-text">v-show的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-bind的使用"><span class="toc-text">v-bind的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#v-for的使用"><span class="toc-text">v-for的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#v-model的使用"><span class="toc-text">v-model的使用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#案例：简单记事本的实现"><span class="toc-text">案例：简单记事本的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#新增"><span class="toc-text">新增</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#axios的使用"><span class="toc-text">axios的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#跨域的问题："><span class="toc-text">跨域的问题：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#一、为什么会出现跨域问题"><span class="toc-text">一、为什么会出现跨域问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#二、什么是跨域"><span class="toc-text">二、什么是跨域</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#三、Django解决跨域问题"><span class="toc-text">三、Django解决跨域问题</span></a></li></ol></li></ol>
  </li></div>


  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2018/12/09/云计算历史/" rel="next" title="云计算历史">
          云计算历史
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2019/03/04/最好的职业规划/" rel="prev" title="最好的职业规划">
            最好的职业规划
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
